﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Data;
using System.Data.SqlClient;
namespace TrainManage.Controllers
{
    public class MaintainController : Controller
    {
        //
        // GET: /Maintain/维修控制器
        MaintainBll mtbll = new MaintainBll();
        RepairsBll repbll = new RepairsBll();

        /// <summary>
        /// 初始化接单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            SqlParameter par = new SqlParameter();
            par.Value = 3;
            par.ParameterName = "ExamineState";
            SqlParameter par1 = new SqlParameter();
            par1.Value = 0;
            par1.ParameterName = "MaintainState";
            return View(repbll.ShowPair(par, par1));
        }

        /// <summary>
        /// 接单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Add(int id)
        {
            Repairs rep = repbll.Back(id);
            rep.MaintainState = 1;
            repbll.UpdPairs(rep, rep.RepairsId);
            Maintain m = new Maintain();
            m.MaintainPeople = Session["UserName"].ToString();
            m.RepairsId = id;
            m.PingXing = 0;
            m.MaintainInfo = "";
            if (mtbll.AddMaintain(m))
            {
                return Content("<script>alert('接单成功');location.href='/Maintain/MyIndent';</script>");
            }
            else
            {
                return Content("<script>alert('接单失败');location.href='/Maintain/Index';</script>");
            }

        }

        /// <summary>
        /// 我的维修
        /// </summary>
        /// <returns></returns>
        public ActionResult MyIndent(int PageIdenx=1)
        {
            string UserName = Session["UserName"].ToString();
            int PageSize = 5;
            int PageCount = 0;
            var list = from a in mtbll.PaperMaintain(UserName, PageIdenx, PageSize, out PageCount)
                       join b in repbll.ShowPair()
                       on a.RepairsId equals b.RepairsId
                       orderby a.MaintainId descending
                       select Tuple.Create(
                          a.MaintainId,
                           b.RailWayName,
                           b.RepairsName,
                           b.FindDate,
                           b.RapairsPeople,
                           b.MaintainState,
                           Tuple.Create(
                           a.PingXing,
                           a.RepairsId,
                           a.MaintainPeople
                          
                           )
                       );
            ViewBag.list = list.Where(t=>t.Item7.Item3==Session["UserName"].ToString());
            ViewBag.TotalPage = (int)Math.Ceiling((double)PageCount / (double)PageSize);
            ViewBag.PageIndex = PageIdenx;
            ViewBag.PageSize = PageSize;
            return View();
        }

        /// <summary>
        /// 修改报修表中的状态为为维修中
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UpdateMaintain()
        {
            int id = Convert.ToInt32(Request["id"]);
            int mid = Convert.ToInt32(Request["mid"]);
            Session["mid"] = mid;
            Repairs rep = repbll.ShowPair().Where(t => t.RepairsId == id).FirstOrDefault();
            rep.MaintainState = 2;
            if (repbll.UpdPairs(rep, id))
            {
                return Content("<script>location.href='/Maintain/MaintainDetail/?id=" + id + "'</script>");
            }
            else
            {
                return Content("<script>location.href='/Maintain/MyIndent'</script>");
            }

        }

        /// <summary>
        /// 维修详情
        /// </summary>
        /// <returns></returns>
        public ActionResult MaintainDetail(int id)
        {
            Session["mid"] = Session["mid"];
            var rep = repbll.ShowPair().Where(t => t.RepairsId == id).FirstOrDefault();
            return View(rep);
        }

        /// <summary>
        /// 维修完成与保存
        /// </summary>
        /// <returns></returns>
        public bool Savechange(int PageIdenx = 1)
        {
            int PageSize = 5;
            int PageCount = 0;
            var state = Request["state"];
            var id = Request["rid"];
            var content = Request["content"];
            string UserName = Session["UserName"].ToString();
            if(Convert.ToInt32(state)==1)
            {
                //修改维修记录
                var main = mtbll.PaperMaintain(UserName, PageIdenx, PageSize, out PageCount).Where(t => t.MaintainId == Convert.ToInt32(id)).FirstOrDefault();
                main.MaintainInfo = content;

                if(mtbll.UpdateMain(main,Convert.ToInt32(id)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
                if(Convert.ToInt32(state)==2)
                {
                    //修改维修记录
                    var main = mtbll.PaperMaintain(UserName, PageIdenx, PageSize, out PageCount).Where(t => t.MaintainId == Convert.ToInt32(id)).FirstOrDefault();
                    main.MaintainInfo = content;
                     //修改完成
                    var rep=repbll.ShowPair().Where(t=>t.RepairsId==main.RepairsId).FirstOrDefault();
                    rep.MaintainState=3;
                    if (mtbll.UpdateMain(main, Convert.ToInt32(id)))
                    {
                        if (repbll.UpdPairs(rep,main.RepairsId))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                  
                }else
                {
                    return false;
                }
        }
    }
}
